package cn.lds.j367.dsbank.controller;

import cn.lds.j367.dsbank.common.dto.ResultDTO;
import cn.lds.j367.dsbank.entity.Employee;
import cn.lds.j367.dsbank.entity.Role;
import cn.lds.j367.dsbank.entity.Syslog;
import cn.lds.j367.dsbank.exception.SysException;
import cn.lds.j367.dsbank.service.EmpService;
import cn.lds.j367.dsbank.service.RoleService;
import cn.lds.j367.dsbank.service.impl.SyslogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;

@Controller
@RequestMapping("/emp")
public class EmpController extends BaseController{

    @Autowired
    private EmpService empService;

    @Autowired
    private RoleService roleService;

//    @Autowired
//    private SyslogService syslogService;

    private static final Logger log = LoggerFactory.getLogger(EmpController.class);

    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    @RequestMapping("/show_emp")
    public String showEmp(Model model){

        System.out.println(model.getClass());
//
//        Syslog syslog = new Syslog();
//        //获取登录员工id
//        Integer empId = this.getLoginEmp().getEmpId();
//        //获取访问的时间
//        Date now = new Date();
//        //获取访问的url
//        String url = "/emp/show_emp";
//
//        String methodName = "showEmp";
//
//        //获取访问IP
//        String localAddr = request.getLocalAddr();
//        syslog.setEmpId(empId);
//        syslog.setIpAddress(localAddr);
//        syslog.setMethodName(methodName);
//        syslog.setVisitUrl(url);
//        syslog.setOperatorTime(now);
//
//        syslogService.addSyslog(syslog);


        ResultDTO<List<Employee>> listResultDTO = empService.selectEmployeeByEmp(null);

        log.info("员工信息：{}",listResultDTO);

        model.addAttribute("empResult",listResultDTO);


        return "emp/show_emp";
    }

    @RequestMapping("/show_emp/{empId}")
    public String showEmpById(@PathVariable() Integer empId,Model model){

        ResultDTO<Employee> employeeResultDTO = empService.selectEmpById(empId);

        log.info("员工详情：{}",employeeResultDTO);

        model.addAttribute("empResult",employeeResultDTO);

        return "emp/show_one_emp";
    }

    @RequestMapping("/add_emp")
    public String addEmp(Model model){
        ResultDTO<List<Role>> listResultDTO = roleService.selectRoleAll();

        if (listResultDTO.getCode() == 2000){
            model.addAttribute("roles",listResultDTO.getObj());
        }

        return "emp/add_emp";
    }

    @RequestMapping("/do_add_emp")
    public String doAddEmp(@Valid Employee employee, BindingResult errors, Integer[] roles) throws SysException {

//        log.info("提交数据是否有错误：{}",errors.hasErrors());
        log.info("参数类型：{}",errors.getClass());
        log.info("参数类型：{}",roles.getClass());

        if (errors.hasErrors()){
            List<FieldError> fieldErrors = errors.getFieldErrors();

            for (FieldError fieldError : fieldErrors) {
                System.out.println(fieldError.getField() + ":"+fieldError.getDefaultMessage());
                //获取错误名
                String errorName = fieldError.getField();
                //获取错误信息
                String errorMsg = fieldError.getDefaultMessage();
                //抛出异常
                throw new SysException(fieldError.getField() + ":"+fieldError.getDefaultMessage());
            }
        }

        employee.setCreateEmpId(this.getLoginEmp().getEmpId());
        log.info("添加员工：{}",employee );
        Stream.of(roles).forEach(System.out::println);

        ResultDTO resultDTO = empService.addEmp(employee, roles);


        return "redirect:/emp/show_emp";
    }
}
